class Solution
{
public:
    int longestDecomposition(string text)
    {
        int front = 0;
        int back = text.size() - 1;
        int count = 0;
        while (front <= back)
        {
            int nextFront = front;
            int nextBack = back;
            while (nextFront < nextBack)
            {
                bool rangeEqual = true;
                for (int i = 0; i + front <= nextFront; ++i)
                {
                    if (text[front + i] != text[nextBack + i])
                    {
                        rangeEqual = false;
                        break;
                    }
                }
                ++nextFront;
                --nextBack;
                if (rangeEqual)
                {
                    count += 2;
                    break;
                }
                else if (nextFront > nextBack)
                {
                    count += 1;
                    break;
                }
            }
            if (nextFront == nextBack)
            {
                count += 1;
                break;
            }
            front = nextFront;
            back = nextBack;
        }
        return count;
    }
};